package com.testing.aggregate;

/**
 * @author lzp
 */
public class TestClone implements Cloneable{

    private int a;
    private int b;
    private int[] ars;

    public TestClone(){
        ars = new int[10];
        for (int i = 0; i < 10; i++) {
            ars[i] = i;
        }
    }

    public int getAr(int index){
        if (index < ars.length && index > 0){
            return ars[index];
        }else {
            return 0;
        }
    }

    public void setArs(int index,int value){
        if (index < ars.length && index > 0){
            ars[index] = value;
        }
    }

    @Override
    protected TestClone clone() throws CloneNotSupportedException {
        return (TestClone)super.clone();
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        // 浅拷贝
        TestClone testClone = new TestClone();
        TestClone testClone1 = null;
        testClone1 = testClone.clone();
        // 都是指向同一个对象
        testClone.setArs(1,2);
        System.out.println(testClone1.getAr(1));
        // 如果类没有重写clone方法并实现Cloneable接口，运行时就会报错
        //testClone.clone();
        // 深拷贝时clone方法必须复制原有对应的数据，并开辟新的内存空间

    }
}
